from flask import Flask
from redis import StrictRedis
from flask_apscheduler import APScheduler
from config import Config, config_dict
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow

scheduler = APScheduler()
redis_store = None

db = SQLAlchemy()
ma = Marshmallow()

def create_app(config_name):
    app = Flask(__name__, static_folder='../static', template_folder='../static')
    config = config_dict[config_name]
    app.config.from_object(config)

    scheduler.init_app(app)
    scheduler.start()

    global redis_store
    redis_store = StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT, password=config.REDIS_PASSWORD, decode_responses=True)

    global db
    db.init_app(app)

    global  ma
    ma.init_app(app)

    from views import kpl, sdata,my_scheduler,position
    app.register_blueprint(position, url_prefix='/position')
    app.register_blueprint(kpl, url_prefix='/kpl')
    app.register_blueprint(sdata, url_prefix='/sdata')
    app.register_blueprint(my_scheduler, url_prefix='/scheduler')


    return app
    